package com.herench.bunch.clinic.handler;

import com.alibaba.fastjson.JSON;
import com.herench.bunch.clinic.agent.ClinicAgent;
import com.herench.bunch.clinic.convert.HospitalBillConvert;
import com.herench.bunch.clinic.support.ProxyMsgTemplate;
import com.herench.bunch.commons.annotation.Handler;
import com.herench.bunch.commons.support.AbstractMessageHandler;
import org.dom4j.Document;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Handler(code = "200270", name = "查询住院缴费清单" ,messageConvertClass = HospitalBillConvert.class)
public class HospitalBillHandlerClinic extends AbstractMessageHandler {


    @Autowired
    private ClinicAgent clinicAgent;

    @Override
    protected Object proxyInvoke(Object inMessage) throws Exception {
        Document message = (Document) inMessage;
        Document doc = ProxyMsgTemplate.getOutMsgTpl(message.asXML());
        Map<String,Object> params = new HashMap<>();
        String startTime = doc.getRootElement().elementTextTrim("startTime");
        String endTime = doc.getRootElement().elementTextTrim("endTime");
        String cardType = doc.getRootElement().elementTextTrim("cardType");
        String cardNo = doc.getRootElement().elementTextTrim("cardNo");
        if ("1".equals(cardType)){
            params.put("cardNo",cardNo);
        }else if("2".equals(cardType)){
            params.put("insurNo",cardNo);
        }

        if("".equals(startTime) || startTime.isEmpty()){
            params.put("record","2");
        }else {
            params.put("record","1");
            params.put("startTime",startTime);
            params.put("endTime",endTime);
        }
        List<Map<String,Object>> res = clinicAgent.HospitalBill(params);
        String req = JSON.toJSONString(res);
        return req;
    }
}
